<template>
  <div class="order-container">
    <div class="order-top">
      <h2 class="title">
        <i class="el-icon-s-data"></i>订单统计
      </h2>
      <ul>
        <li>
          <p>
            <span>¥</span>
            <span>{{orderList.today_order_amount}}</span>
          </p>
          <p>今日销售额</p>
        </li>
        <li>
          <p style="color:red">{{orderList.month_order_number}}</p>
          <p>当月订单总数</p>
        </li>
        <li>
          <p style="color:red !important">
            <span>¥</span>
            <span>{{orderList.month_sale}}</span>
          </p>
          <p>当月销售总额</p>
        </li>
        <li>
          <p>{{orderList.total_number}}</p>
          <p>订单总数</p>
        </li>
        <li>
          <p>
            <span>¥</span>
            <span>{{orderList.total_sale_amount}}</span>
          </p>
          <p>销售总额</p>
        </li>
      </ul>
    </div>
    <div class="bottom">
      <div class="bottom-top">
        <my-input class="my-input" @search="mySearch" :placeholder="placeholder"></my-input>
        <div class="menu">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="订单状态:">
              <el-select v-model="form.status" placeholder="请选择" @change="selecteStatus">
                <el-option
                  v-for="item in optionsStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="订单类型:">
              <el-select v-model="form.type" placeholder="请选择" @change="selectType">
                <el-option
                  v-for="item in optionsType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <el-button class="export-order" @click.native="handleExportOrder">导出订单</el-button>
      </div>
      <div class="bottom-content">
        <Table
          @viewDetail="handleDetail"
          @currentChange="currentChange"
          :tableData="tableData"
          :total="total"
          :currentPage="currentPage"
          :page="page"
        ></Table>
      </div>
    </div>
    <view-detail ref="viewDetail" :order_id="order_id"></view-detail>
  </div>
</template>
<script>
import myInput from "@/common/input/index";
import Table from "@/views/order/table";
import ViewDetail from "@/views/order/detail";
import { orderStatistics, getOrderList, exportOrder } from "@/api/operation";
import { ERR_OK } from "@/api/config";
export default {
  data() {
    return {
      form: {
        status: "",
        type: ""
      },
      optionsType: [
        {
          value: 1,
          label: "班级课程"
        },
        {
          value: 2,
          label: "线下盒子"
        },
        {
          value: 3,
          label: "课程和线下盒子"
        }
      ],
      optionsStatus: [
        {
          value: 1,
          label: "待支付"
        },
        {
          value: 2,
          label: "待发货"
        },
        {
          value: 3,
          label: "待收货"
        },
        {
          value: 4,
          label: "待评价"
        },
        {
          value: 5,
          label: "订单完成"
        },
        {
          value: 6,
          label: "取消订单"
        }
      ],
      orderList: {},
      tableData: [],
      total: 0,
      currentPage: 1,
      page: 1,
      keyword: "",
      placeholder: "请输入订单号或用户名",
      flag: 0,
      order_status: 0,
      order_type: 0,
      order_id: ""
    };
  },
  methods: {
    handleExportOrder() {
      this.flag = 1;
      this._exportOrder(
        this.page,
        this.keyword,
        this.flag,
        this.order_status,
        this.order_type
      );
    },
    selectType(val) {
      console.log(val);
      this.page = 1;
      this.order_type = val;
      this.currentPage = 1;
      this.flag = 0;
      this._getOrderList(
        this.page,
        this.keyword,
        this.flag,
        this.order_status,
        this.order_type
      );
    },
    selecteStatus(val) {
      console.log(val);
      this.page = 1;
      this.order_status = val;
      this.currentPage = 1;
      this.flag = 0;
      this._getOrderList(
        this.page,
        this.keyword,
        this.flag,
        this.order_status,
        this.order_type
      );
    },
    currentChange() {
      this.page = val;
      this.currentPage = 1;
      this._getOrderList(this.page);
    },
    mySearch(keyword) {
      this.keyword = keyword;
      this.page = 1;
      this.currentPage = 1;
      this._getOrderList(this.page, this.keyword);
    },
    handleDetail() {
      this.$refs.viewDetail.dialogTableVisible = true;
      this.order_id = localStorage.getItem("ORDER_ITEM");
      // console.log(this.order_id);
    },
    _orderStatistics() {
      orderStatistics()
        .then(res => {
          if (res.code == ERR_OK) {
            // console.log(res);
            this.orderList = res.list;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    _getOrderList(page, keyword, flag, order_status, order_type) {
      getOrderList(page, keyword, flag, order_status, order_type)
        .then(res => {
          if (res.code == ERR_OK) {
            console.log(res);
            this.tableData = res.list.data;
            this.total = res.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    _exportOrder(page, keyword, flag, order_status, order_type) {
      exportOrder(page, keyword, flag, order_status, order_type)
        .then(res => {
          if (res.code == ERR_OK) {
            let b = new Blob([res], { type: "text/html; charset=UTF-8" });
            let url = URL.createObjectURL(b);
            let link = document.createElement("a");
            link.download = "芽呀呀订单列表’.xlsx";
            link.href = url;
            link.click();
            link.remove();
            window.URL.revokeObjectURL(url);
            // console.log(res)
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  created() {
    this._orderStatistics();
    this._getOrderList();
  },
  components: {
    myInput,
    Table,
    ViewDetail
  }
};
</script>
<style lang="scss" scoped>
.order-container {
  padding: 20px;
  padding-top: 50px;
  .order-top {
    width: 100%;
    height: 200px;
    .title {
      color: rgba(51, 51, 51, 1);
      font-size: 16px;
      font-family: Roboto;
      font-weight: bold;
      i {
        width: 20px;
        height: 20px;
        background-color: rgba(102, 102, 102, 1);
        text-align: center;
        line-height: 20px;
        color: #fff;
        margin-right: 9px;
      }
    }
    ul {
      width: 100%;
      display: flex;
      justify-content: space-around;
      list-style: none;
      margin-top: 50px;
      li {
        &:nth-child(1) {
          color: rgba(129, 148, 204, 1);
        }
        p {
          margin: 0;
          &:nth-child(1) {
            font-size: 36px;
            width: 92px;
            height: 52px;
            span {
              text-align: center;
              font-family: Roboto;
              // color: rgba(129, 148, 204, 1);
              &:nth-child(1) {
                width: 21px;
                height: 25px;
                line-height: 25px;
                font-size: 18px;
                position: relative;
                top: -8px;
                margin-right: 5px;
              }
              &:nth-child(2) {
                width: 92px;
                height: 52px;
                line-height: 52px;
                font-size: 36px;
              }
            }
          }
          &:nth-child(2) {
            color: #000;
          }
        }
      }
    }
  }
  .bottom {
    .bottom-top {
      display: flex;
      position: relative;
      .my-input {
        padding: 0;
        margin-right: 34px;
      }
      .menu {
        .el-form {
          display: flex;
        }
      }
      .export-order {
        width: 87px;
        height: 30px;
        line-height: 20px;
        border-radius: 6px;
        background-color: rgba(27, 166, 135, 1);
        text-align: center;
        padding: 0;
        color: #fff;
        position: absolute;
        right: 0px;
      }
    }
  }
}
</style>